package com.whfc.iot.fse.crane.codec;

import com.whfc.common.util.MathUtil;
import com.whfc.iot.fse.crane.entity.TwMsgParam;
import com.whfc.iot.util.ByteUtil;
import com.whfc.iot.util.MsgDataCodecUtil;
import io.netty.buffer.ByteBuf;

public class TwMsgDataCodecParam implements TwMsgDataCodec<TwMsgParam>{
    @Override
    public void decode(ByteBuf buf, TwMsgParam msg) {
        // 坐标x
        int coord_x = buf.readUnsignedShort();
        // 坐标y
        int coord_y = buf.readUnsignedShort();
        // 起重臂长(前臂长)
        int qzbc = buf.readUnsignedShort();
        // 平衡臂长（后臂长）
        int phbc = buf.readUnsignedShort();
        // 塔帽高度
        int tmgd = buf.readUnsignedShort();
        // 塔身高度（大臂高）
        int tsgd = buf.readUnsignedShort();
        // 最大吊重
        int maxWeight = buf.readUnsignedShort();
        // 最大力矩
        int maxMomentRatio = buf.readUnsignedShort();
        // 产权状态
        int equityState = buf.readUnsignedByte();
        // 塔机型号
        String craneType= MsgDataCodecUtil.readToString(buf,30);
        // 生产厂商
        String makeFirm= MsgDataCodecUtil.readToString(buf,30);
        // 吊钩重量
        int liftHookWeight= buf.readUnsignedShort();
        // 铰链长度
        int hingeLength = buf.readUnsignedShort();
        // 传感器安装状态 0bit高度; 1bit幅度; 2bit回转; 3bit重量; 4bit风速; 5bit倾斜;  0011 1111应该是全部安装
        int sensorInstallState = buf.readUnsignedByte();
        // 高度
        int hightSensorState = ByteUtil.getBit(sensorInstallState, 0);
        // 幅度
        int rangeSensorState = ByteUtil.getBit(sensorInstallState, 1);
        // 回转
        int rotationSensorState = ByteUtil.getBit(sensorInstallState, 2);
        // 重量
        int weightSensorState = ByteUtil.getBit(sensorInstallState, 3);
        // 风速
        int windSpeedSensorState = ByteUtil.getBit(sensorInstallState, 4);
        // 倾斜
        int tiltSensorState = ByteUtil.getBit(sensorInstallState, 5);

        msg.setCoord_x(MathUtil.round(0.1*coord_x,1));
        msg.setCoord_y(MathUtil.round(0.1*coord_y,1));
        msg.setQzbc(MathUtil.round(0.1*qzbc,1));
        msg.setPhbc(MathUtil.round(0.1*phbc,1));
        msg.setTmgd(MathUtil.round(0.1*tmgd,1));
        msg.setTsgd(MathUtil.round(0.1*tsgd,1));
        msg.setMaxWeight(MathUtil.round(0.001*maxWeight,3));
        msg.setMaxMomentRatio(MathUtil.round(0.1*maxMomentRatio,1));
        msg.setEquityState(equityState);
        msg.setCraneType(craneType);
        msg.setMakeFirm(makeFirm);
        msg.setLiftHookWeight(1.0*liftHookWeight);
        msg.setHingeLength(MathUtil.round(0.1*hingeLength,1));
        msg.setSensorInstallState(sensorInstallState);
        msg.setHightSensorState(hightSensorState);
        msg.setRangeSensorState(rangeSensorState);
        msg.setRotationSensorState(rotationSensorState);
        msg.setWeightSensorState(weightSensorState);
        msg.setWindSpeedSensorState(windSpeedSensorState);
        msg.setTiltSensorState(tiltSensorState);
    }

    public static void main(String[] args) {
        System.out.println(0x0BB8);
        System.out.println(0x0200);
        System.out.println(0x0078);
        System.out.println(0x01F4);
        System.out.println(0x1F40);
        System.out.println(0x3F);
    }
}
